﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Damany.Imaging.Common;
using Gallio.Framework;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
using Damany.Cameras;
using Damany.Imaging.Handlers;
using Damany.Imaging.Processors;
using OpenCvSharp;

namespace Damany.Imaging.Processors.Test
{
    [TestFixture]
    public class PortraitFinderTest
    {
        [Test]
        [Timeout(1 * 60 * 60)]
        public void Test()
        {
            //var filters = new[] { new FaceVerifier() };
            var portraitFinder = new PortraitFinder();
            var config = new ConfigurationHandlers.FaceSearchConfigSectionHandler();
            config.MinFaceWidth = 50;
            config.MaxFaceWidth = 300;
            portraitFinder.Configuration = config;
            //portraitFinder.PostFilters = filters;


            var directory = @"F:\测试图片\靖江";
            var outputDir = Path.Combine(directory, DateTime.Now.ToShortTimeString().Replace(':', '-'));
            var dirImages = Directory.EnumerateFiles(directory, "*.jpg");


            var count = 0;
            foreach (var dirImage in dirImages)
            {
                var img = AForge.Imaging.Image.FromFile(dirImage);
                var frame = new Frame(IplImage.FromBitmap(img));
                frame.MotionRectangles.Add(new CvRect(0, 0, img.Width, img.Height));
                var list = new List<Frame> { frame };
                var portraits = portraitFinder.ProcessFrame(frame, new CancellationToken());
                foreach (var portrait in portraits)
                {
                    if (!Directory.Exists(outputDir))
                    {
                        Directory.CreateDirectory(outputDir);
                    }

                    var path = Path.Combine(outputDir, count.ToString() + ".jpg");
                    portrait.GetIpl().SaveImage(path);
                    ++count;
                }
            }

        }
    }


}
